1 00:00:00,570 --> 00:00:06,030 In this video, I'm going to introduce event handling and I'm going to connect my click detector right 2 00:00:06,030 --> 00:00:09,510 here that we're going to create to a make steps function. 3 00:00:09,510 --> 00:00:13,110 So when we click it, boom, we make steps. 4 00:00:13,110 --> 00:00:14,070 That's pretty cool. 5 00:00:14,070 --> 00:00:16,200 And you can click it again and make more steps. 6 00:00:16,200 --> 00:00:19,710 And I do not have a limit on it, but I do have a D bounce. 7 00:00:19,710 --> 00:00:22,230 So while the steps are being made, it can't be pressed. 8 00:00:22,230 --> 00:00:24,180 Again, I want to show you that too. 9 00:00:24,180 --> 00:00:26,340 So let's go ahead and get started with that. 10 00:00:27,670 --> 00:00:27,920 All right. 11 00:00:28,030 --> 00:00:30,010 So this is a world from my last video. 12 00:00:30,010 --> 00:00:32,410 It only has a single script in it. 13 00:00:32,410 --> 00:00:34,750 And the script is to make steps. 14 00:00:34,780 --> 00:00:36,220 We can take a look at it real quick. 15 00:00:36,220 --> 00:00:39,820 I will put it in the description in case you didn't see the last video. 16 00:00:39,820 --> 00:00:43,300 But we do need a function and something to handle our click event. 17 00:00:43,300 --> 00:00:44,710 So I thought this would be good. 18 00:00:45,070 --> 00:00:47,530 All right, so we have a function to make steps. 19 00:00:47,530 --> 00:00:53,200 What we don't have is a function to call in order to create all the steps. 20 00:00:53,200 --> 00:00:54,610 We just have this loose code here. 21 00:00:54,610 --> 00:00:57,340 We have a for loop that creates a bunch of steps. 22 00:00:57,640 --> 00:00:59,140 All right, we can fix that. 23 00:00:59,140 --> 00:01:02,140 Let's go back to our world and make our button. 24 00:01:02,410 --> 00:01:02,770 Right. 25 00:01:02,770 --> 00:01:07,300 So we are going down the Z axis on our steps. 26 00:01:07,540 --> 00:01:15,070 Let's go ahead and face this way to make our click detector go to part out of block. 27 00:01:15,640 --> 00:01:16,540 Here's my block. 28 00:01:16,540 --> 00:01:17,950 I'll make it a little bigger. 29 00:01:18,340 --> 00:01:21,160 This is going to be the stand that holds the button. 30 00:01:21,670 --> 00:01:23,380 Always just make it a little bit bigger like that. 31 00:01:23,410 --> 00:01:24,580 That's pretty good. 32 00:01:25,300 --> 00:01:25,960 Cool. 33 00:01:25,990 --> 00:01:31,930 Let's call this stand and we've got to make sure that it's anchored. 34 00:01:31,930 --> 00:01:36,250 You can color it if you want, but we don't want it to fall over anchored. 35 00:01:36,280 --> 00:01:36,910 Cool. 36 00:01:36,940 --> 00:01:40,360 Now I want to put a button on that. 37 00:01:40,360 --> 00:01:42,070 So I don't want to be facing out here. 38 00:01:42,070 --> 00:01:43,330 I want the button to fly out here. 39 00:01:43,330 --> 00:01:45,070 I probably should have told you that earlier. 40 00:01:45,130 --> 00:01:46,780 You probably figured it out by now. 41 00:01:46,930 --> 00:01:49,270 I'm going to go ahead and hit a cylinder. 42 00:01:49,270 --> 00:01:50,590 This is going to be my button. 43 00:01:50,590 --> 00:01:51,880 I'm going to call it button. 44 00:01:51,880 --> 00:01:55,030 I know it doesn't look like a button, but we will fix that. 45 00:01:55,510 --> 00:01:56,290 There we go. 46 00:01:56,290 --> 00:01:58,390 Now I'm going to make that button green. 47 00:01:59,340 --> 00:02:02,090 Brick color, really green so I can see it. 48 00:02:02,100 --> 00:02:03,270 Lime green. 49 00:02:04,000 --> 00:02:06,850 And then let's go down to the size. 50 00:02:06,870 --> 00:02:08,640 It's too long, right? 51 00:02:08,670 --> 00:02:09,650 See that red? 52 00:02:09,660 --> 00:02:11,480 That means it's on the x axis. 53 00:02:11,490 --> 00:02:14,030 And you could see that here for one one. 54 00:02:14,040 --> 00:02:17,490 I'm going to make it 0.211 for the size. 55 00:02:17,880 --> 00:02:18,420 There we go. 56 00:02:18,420 --> 00:02:19,260 It's nice and thin. 57 00:02:19,290 --> 00:02:21,120 Don't make it too small or your phone. 58 00:02:21,120 --> 00:02:22,740 People are never going to be able to click on it. 59 00:02:23,220 --> 00:02:23,400 All right. 60 00:02:23,400 --> 00:02:28,380 Let's go to rotate and let's make it like that. 61 00:02:29,010 --> 00:02:33,480 I'm going to have it centered on my my my stand. 62 00:02:33,480 --> 00:02:35,280 So I'm turning collisions off. 63 00:02:36,170 --> 00:02:38,210 And then I'm going to go to stand. 64 00:02:39,080 --> 00:02:49,040 I'm going to go position control C to copy the position, get my button control V to paste the position 65 00:02:49,190 --> 00:02:52,040 of the stand into the button now directly in the center. 66 00:02:52,070 --> 00:02:54,800 That's why I turn collisions off when I hit the move. 67 00:02:55,400 --> 00:02:57,950 Drag that out, hit the collisions. 68 00:02:58,580 --> 00:02:59,690 Bump that up against. 69 00:02:59,690 --> 00:03:03,110 If you push too hard, it'll go through, but you'll feel a bump, right? 70 00:03:03,740 --> 00:03:05,840 And we can move this up a little bit. 71 00:03:06,320 --> 00:03:07,280 That's pretty cool. 72 00:03:07,310 --> 00:03:08,210 That's our button. 73 00:03:08,210 --> 00:03:10,220 And we do have it anchored, right? 74 00:03:11,390 --> 00:03:13,670 Oh, we do not anchor it so it doesn't fall. 75 00:03:13,670 --> 00:03:15,260 If it falls down, it's not anchored. 76 00:03:16,040 --> 00:03:18,020 All right, so we got our stand. 77 00:03:18,740 --> 00:03:19,760 I'm going to hit a control. 78 00:03:19,760 --> 00:03:21,530 Click on the button control. 79 00:03:21,530 --> 00:03:22,790 Click on the steps. 80 00:03:22,790 --> 00:03:25,400 This is everything we need for our make steps. 81 00:03:25,400 --> 00:03:28,550 Let's right click and then group. 82 00:03:29,300 --> 00:03:30,100 Cool. 83 00:03:30,140 --> 00:03:31,880 Now I'm going to call this. 84 00:03:31,880 --> 00:03:32,720 It's a model, right? 85 00:03:32,720 --> 00:03:35,060 It's a it's a subcategory of folder. 86 00:03:35,060 --> 00:03:36,590 It's a specialized folder. 87 00:03:36,590 --> 00:03:38,810 I'm going to call this steps. 88 00:03:38,810 --> 00:03:42,260 That way when we open steps up, we see all of our stuff in there. 89 00:03:42,260 --> 00:03:43,880 Nice little package. 90 00:03:44,630 --> 00:03:45,440 Cool. 91 00:03:45,470 --> 00:03:47,540 Let's go ahead and open steps up. 92 00:03:47,540 --> 00:03:48,080 Oh, you know what? 93 00:03:48,080 --> 00:03:49,820 We've got to put a click detector on our button. 94 00:03:50,120 --> 00:03:53,750 Get our button plus click detector. 95 00:03:53,750 --> 00:03:54,580 There we go. 96 00:03:54,590 --> 00:03:55,580 That would have been bad. 97 00:03:55,580 --> 00:03:56,690 We would have forgot it. 98 00:03:56,690 --> 00:03:57,930 We would have been confused. 99 00:03:57,950 --> 00:03:59,420 Let's do steps. 100 00:04:00,020 --> 00:04:00,950 Open that up. 101 00:04:01,460 --> 00:04:02,060 Cool. 102 00:04:02,090 --> 00:04:04,400 Let's get a reference for our click detector. 103 00:04:04,400 --> 00:04:06,200 It is inside the model. 104 00:04:06,650 --> 00:04:09,020 But we can do that. 105 00:04:09,020 --> 00:04:09,980 We can get that. 106 00:04:09,980 --> 00:04:10,970 I'm going to call it click. 107 00:04:10,970 --> 00:04:13,130 You can call the button, whatever makes sense. 108 00:04:13,670 --> 00:04:16,460 And then it's script dot parent, right? 109 00:04:16,460 --> 00:04:18,620 That takes us to the model level. 110 00:04:18,620 --> 00:04:25,830 Then we got to go down a level to button, dot button, and then down another level to click detector. 111 00:04:25,850 --> 00:04:32,120 This takes a little while to get used to this type of of moving around, trying to get different components. 112 00:04:32,120 --> 00:04:33,950 But once you get it, it won't be too bad. 113 00:04:35,030 --> 00:04:35,390 Cool. 114 00:04:35,390 --> 00:04:37,040 We got our click detector. 115 00:04:37,730 --> 00:04:38,870 We can get rid of this. 116 00:04:38,870 --> 00:04:43,610 Wait, because we're going to fire off our make steps, which is essentially going to be this inside 117 00:04:43,610 --> 00:04:46,850 a function using an event handler. 118 00:04:46,850 --> 00:04:47,210 Right. 119 00:04:47,210 --> 00:04:50,330 When we click the button so we don't have to wait to get into the world. 120 00:04:50,720 --> 00:04:54,440 Let's make a function right here, say a local function. 121 00:04:54,530 --> 00:04:57,530 I'm going to call this make steps. 122 00:04:57,530 --> 00:04:59,360 So the first function was make step. 123 00:04:59,360 --> 00:05:02,000 Without the SW, this is going to make a whole bunch of steps. 124 00:05:02,010 --> 00:05:02,630 Right? 125 00:05:02,630 --> 00:05:07,490 I'm going to come down here, I'm going to put another end and my formatting is all messed up. 126 00:05:07,490 --> 00:05:09,380 So I'll go to format selection. 127 00:05:09,380 --> 00:05:13,010 I'm under script format, selection, format, document. 128 00:05:13,190 --> 00:05:14,240 Ooh, look at that. 129 00:05:14,240 --> 00:05:17,900 All of my code blocks are formatted correctly. 130 00:05:18,440 --> 00:05:19,310 Cool beans. 131 00:05:19,310 --> 00:05:22,960 Now we need to call this right. 132 00:05:23,270 --> 00:05:25,940 So I'm going to go down below where I want to call it. 133 00:05:25,940 --> 00:05:27,170 I'm going to get my click. 134 00:05:27,170 --> 00:05:29,450 Remember, that's my click detector dot. 135 00:05:29,630 --> 00:05:30,950 Oh, see that lightning bolt? 136 00:05:31,250 --> 00:05:33,230 That means it's an event, right? 137 00:05:33,230 --> 00:05:37,070 And that's universal across a lot of different environments, not just road blocks. 138 00:05:37,070 --> 00:05:38,930 You'll see that in other things too. 139 00:05:38,930 --> 00:05:43,430 Like, you know, when you're doing C-sharp programming, they show your events like that. 140 00:05:43,430 --> 00:05:50,780 Let's double click it, bam, cool, hit a colon, connect, make steps. 141 00:05:51,050 --> 00:05:58,490 If your ID, if you're if your environment puts parentheses in here like this, you got to get rid of 142 00:05:58,490 --> 00:06:01,310 them when you call the function. 143 00:06:03,040 --> 00:06:05,460 Via a connector from an event. 144 00:06:05,470 --> 00:06:08,110 You only put the function name in there. 145 00:06:09,400 --> 00:06:09,850 All right. 146 00:06:09,850 --> 00:06:11,770 And we'll do more with arguments later. 147 00:06:11,770 --> 00:06:14,650 Just know that you can call this from here. 148 00:06:14,650 --> 00:06:17,020 Just the name, not the parentheses. 149 00:06:17,020 --> 00:06:18,670 All right, we could try this out. 150 00:06:18,670 --> 00:06:20,500 We could still spam it, though. 151 00:06:20,500 --> 00:06:25,090 We don't have anybody anything limiting how many times you hit this button or how fast. 152 00:06:25,310 --> 00:06:27,550 Let's go to view output. 153 00:06:28,670 --> 00:06:31,940 Hit that all we're making or making steps. 154 00:06:31,940 --> 00:06:32,390 Cool. 155 00:06:32,390 --> 00:06:33,830 We can make more steps. 156 00:06:34,280 --> 00:06:36,690 Cool, but we can spam it. 157 00:06:36,690 --> 00:06:37,270 See, has it going. 158 00:06:37,280 --> 00:06:38,900 One, two, three, four. 159 00:06:38,900 --> 00:06:39,770 Watch this. 160 00:06:40,280 --> 00:06:41,870 Boom, boom, boom, boom, boom, boom, boom. 161 00:06:41,900 --> 00:06:44,030 So you got a lot of different numbers here. 162 00:06:44,420 --> 00:06:45,780 That's crazy, right? 163 00:06:45,800 --> 00:06:47,030 You got a couple nines. 164 00:06:47,030 --> 00:06:48,740 You got ten here, nine there. 165 00:06:49,310 --> 00:06:50,660 You'll crash your machine. 166 00:06:50,660 --> 00:06:54,560 Kids will discover discover how to do that, and then you'll be out of luck. 167 00:06:54,560 --> 00:06:56,750 So we got to put a bounce in there. 168 00:06:57,110 --> 00:07:00,860 I'm going to hit this just to get some more space, turn my game off. 169 00:07:00,860 --> 00:07:05,360 And a bounce is something that limits how many times code could be called. 170 00:07:05,630 --> 00:07:07,220 So that's what we're going to do. 171 00:07:07,250 --> 00:07:08,900 We're going to do a bounce. 172 00:07:09,410 --> 00:07:11,030 It was not invented in robots. 173 00:07:11,030 --> 00:07:12,590 It was invented a long time ago. 174 00:07:12,590 --> 00:07:17,720 It was for electronic circuits, actually for the switches, so that it wouldn't fire multiple times. 175 00:07:18,290 --> 00:07:23,990 Let's go ahead and make a can make four can make steps. 176 00:07:24,560 --> 00:07:29,720 And we're going to say true, this is going to be my flag saying whether we can make the steps or not 177 00:07:29,720 --> 00:07:30,500 can make. 178 00:07:30,530 --> 00:07:32,420 We're going to initially set it to true. 179 00:07:32,660 --> 00:07:37,760 We're going to come down here to our make steps and we are going to do a conditional statement. 180 00:07:37,760 --> 00:07:39,850 We're going to do an if statement, right. 181 00:07:39,860 --> 00:07:46,460 When I say if can make then right? 182 00:07:46,460 --> 00:07:52,340 So so this is this will execute if our condition is true, right? 183 00:07:52,340 --> 00:07:53,030 It is true. 184 00:07:53,030 --> 00:07:54,920 Now can make is equal to true. 185 00:07:55,160 --> 00:08:01,070 Let's go out and get our for loop control x pasted inside here. 186 00:08:01,970 --> 00:08:02,770 Awesome. 187 00:08:03,080 --> 00:08:09,410 I am going to move this end up a little bit so it's a little neater and when we do can make what we 188 00:08:09,410 --> 00:08:16,310 want to do is turn can make to false, which is going to prevent this from firing again until we turn 189 00:08:16,340 --> 00:08:16,970 it true. 190 00:08:17,180 --> 00:08:18,440 Where do we do that? 191 00:08:19,490 --> 00:08:21,020 Right underneath the for loop. 192 00:08:21,050 --> 00:08:25,490 We might want to wait even a second until it's done before we can make more steps. 193 00:08:25,640 --> 00:08:29,870 Then we're going to say can make equals true. 194 00:08:29,900 --> 00:08:34,940 So it'll be false until we're finished making the steps and then we'll make it true. 195 00:08:34,940 --> 00:08:38,120 And you might even want to put a limit to how many times you can loop it. 196 00:08:38,120 --> 00:08:41,330 You're going to learn plenty about that in later videos. 197 00:08:41,330 --> 00:08:43,490 Let's hit our play. 198 00:08:47,450 --> 00:08:51,940 And we'll take a look we'll take a look at our output window just to see if we've got errors. 199 00:08:53,150 --> 00:08:53,560 All right. 200 00:08:53,570 --> 00:08:54,740 It makes our steps. 201 00:08:54,860 --> 00:08:56,320 Can we spam? 202 00:08:56,330 --> 00:08:57,200 I'm going to try it. 203 00:08:57,680 --> 00:08:58,760 Click, click, click, click, click, click. 204 00:08:58,790 --> 00:08:59,480 Look at that. 205 00:08:59,480 --> 00:09:04,910 It's not acting all crazy, but it does start making them again after that one second delay or two second 206 00:09:04,910 --> 00:09:05,420 delay. 207 00:09:05,900 --> 00:09:08,270 But you see, they're all in order. 208 00:09:08,300 --> 00:09:11,990 One, two, three, four, and we'll go from 1 to 10. 209 00:09:11,990 --> 00:09:14,120 We could see how many parts are in the world here. 210 00:09:14,120 --> 00:09:16,520 So that's a lot, right? 211 00:09:16,520 --> 00:09:18,920 I'm going to introduce the debris service soon enough. 212 00:09:18,920 --> 00:09:27,470 But for now, you can make steps, you can capture a click event and then hook it up to a function connected 213 00:09:27,470 --> 00:09:28,250 to a function. 214 00:09:28,250 --> 00:09:31,100 So I will see you in the next video.